﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using lite;
using Csla;
using Csla.Data;
using Csla.Core;
using System.Collections.ObjectModel;
using OpenExpressApp.ORM;
using OpenExpressApp.MetaAttribute;
using OpenExpressApp.DB.Utils;
using OpenExpressApp.MetaModel;


namespace OpenExpressApp.Module.Library
{
    [Serializable]
    public partial class OrgPositionDataPermissions : GBusinessListBase<OrgPositionDataPermissions, OrgPositionDataPermission>
    {
        #region  Factory Methods

        private OrgPositionDataPermissions()
        {
            /* Require use of factory methods */
            AllowNew = true;
        }

        public static OrgPositionDataPermissions GetChild(OrgPosition parent)
        {
            var result = DataPortal.Fetch<OrgPositionDataPermissions>(parent.Id);
            foreach (var item in result)
            {
                item.OrgPosition = parent;
            }
            result.MarkAsChild();
            return result;
        }

        public static OrgPositionDataPermissions NewChild()
        {
            return DataPortal.CreateChild<OrgPositionDataPermissions>();
        }

        protected override object AddNewCore()
        {
            var item = OrgPositionDataPermission.NewChild();
            this.Add(item);
            return item;
        }

        #endregion

        #region  Data Access
        private void DataPortal_Fetch(Guid parentId)
        {
            RaiseListChangedEvents = false;

            using (var db = DBHelper.CreateDb(ConnectionStringNames.OpenExpressApp))
            {
                IQuery q = db.Query();
                q.Constrain("OrgPositionId").Equal(parentId);
                foreach (OrgPositionDataPermission c in db.Select<OrgPositionDataPermission>(q))
                {
                    this.Add(OrgPositionDataPermission.GetChild(c));
                }
            }

            RaiseListChangedEvents = true;
        }



        #endregion
	}

    [Serializable]
    [Table]
    [BusinessObject("e9c5711d-551a-4893-803d-fdbc677347f7"), Label("数据权限")]
    [NotAllowNew]
    public partial class OrgPositionDataPermission : GBusinessBase<OrgPositionDataPermission> 
    {        
        #region  Business Methods

        private static PropertyInfo<Guid> IdProperty =
            RegisterProperty(new PropertyInfo<Guid>("Id"));
        [System.ComponentModel.DataObjectField(true, true)]
        [Column, PK][EntityProperty]
        public Guid Id
        {
            get { return GetProperty(IdProperty); }
            set { SetProperty(IdProperty, value); }
        }
        
	
        private static PropertyInfo<Guid> OrgPositionIdProperty =
         RegisterProperty(new PropertyInfo<Guid>("OrgPositionId"));
        [Column] [EntityProperty][Lookup("OrgPosition")]
        public Guid OrgPositionId
        {
            get { return GetProperty(OrgPositionIdProperty); }
            set { SetProperty(OrgPositionIdProperty, value); }
        }
        [NotUndoable()]
        private OrgPosition orgPosition;
        public OrgPosition OrgPosition
        {
            get { return orgPosition; }
            set 
            { 
				orgPosition = value; 
				PropertyHasChanged("OrgPosition");
			}
        }

        private static PropertyInfo<Guid> ModuleIdProperty = RegisterProperty(new PropertyInfo<Guid>("ModuleId"));
        [Column]
        [Lookup("Module")]
        public Guid ModuleId
        {
            get { return GetProperty(ModuleIdProperty); }
            set { SetProperty(ModuleIdProperty, value); }
        }

        [EntityProperty]
        [ShowInList]
        [Label("模块")]
        public string ModuleName
        {
            get { return Module.Label; }
        }

        [NotUndoable, NonSerialized]
        private BusinessObjectInfo _module;
        public BusinessObjectInfo Module
        {
            get
            {
                if (_module == null)
                    _module = ApplicationModel.GetBusinessObjectInfo(ModuleId);
                return _module;
            }
        }

        private static PropertyInfo<Guid> BusinessObjectIdProperty = RegisterProperty(new PropertyInfo<Guid>("BusinessObjectId"));
        [Column]
        [Lookup("BusinessObjectInfo")]
        public Guid BusinessObjectId
        {
            get { return GetProperty(BusinessObjectIdProperty); }
            set { SetProperty(BusinessObjectIdProperty, value); }
        }

        [EntityProperty]
        [ShowInList]
        [Label("对象")]
        public string BoName
        {
            get { return BusinessObjectInfo.Label; }
        }


        [NotUndoable, NonSerialized]
        private BusinessObjectInfo _businessObjectInfo;
        public BusinessObjectInfo BusinessObjectInfo
        {
            get
            {
                if (_businessObjectInfo == null)
                    _businessObjectInfo = ApplicationModel.GetBusinessObjectInfo(BusinessObjectId);
                return _businessObjectInfo;
            }
        }

        private static PropertyInfo<string> ExpressionProperty =
            RegisterProperty(new PropertyInfo<string>("Expression"));
        [Column]
        [EntityProperty]
        [ShowInListAttribute, Label("可读表达式")]
        public string Expression
        {
            get { return GetProperty(ExpressionProperty); }
            set { SetProperty(ExpressionProperty, value); }
        }

        #endregion
        
        #region  Factory Methods

        public static OrgPositionDataPermission NewChild()
        {
            return DataPortal.CreateChild<OrgPositionDataPermission>();
        }
        
        public static OrgPositionDataPermission GetChild(OrgPositionDataPermission c)
        {
            return DataPortal.FetchChild<OrgPositionDataPermission>(c);
        }

        private OrgPositionDataPermission()
        {          
            /* require use of factory methods */
        }


        #endregion

        #region  Data Access

        protected override void Child_Create()
        {
            LoadProperty(IdProperty, Guid.NewGuid());
            ValidationRules.CheckRules();
        }

        private void Child_Fetch(OrgPositionDataPermission data)
        {
            LoadProperty(IdProperty, data.ReadProperty(IdProperty));
            LoadProperty(OrgPositionIdProperty, data.ReadProperty(OrgPositionIdProperty));
            LoadProperty(ExpressionProperty, data.Expression);

            LoadProperty(ModuleIdProperty, data.ModuleId);
            LoadProperty(BusinessObjectIdProperty, data.BusinessObjectId);
        }

        private void Child_Insert(BusinessBase parent)
        {
            using (var db = DBHelper.CreateDb(ConnectionStringNames.OpenExpressApp))
            {
                db.Insert(this);
            }
        }
        
        private void Child_Update(BusinessBase parent)
        {
            using (var db = DBHelper.CreateDb(ConnectionStringNames.OpenExpressApp))
            {
                db.Update(this);
            }
        }

        private void Child_DeleteSelf(BusinessBase parent)
        {
            using (var db = DBHelper.CreateDb(ConnectionStringNames.OpenExpressApp))
            {
                db.Delete(this);
            }
        }

        #endregion

        #region  Validation Rules

        protected override void AddBusinessRules()
        {

        }

        #endregion
    }
}